package com.yanqu.road.dao.impl.player.cricket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.cricket.*;
import com.yanqu.road.utils.property.PropertyHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserCricketDaoImpl extends TempDataObjectDao<UserCricketData> {

    public UserCricketData getUserCricketData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_cricket_user where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserCricketData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    private static String insertCricketUserDataSql = "INSERT INTO t_u_cricket_user(userId,catchEventTimes,cricketIdList,badgeLevel,cricketScore,scenceId,scenceDataMap,items,totalCatchTimes,currentSeasonId,rankScore,maxRankScore,defendFailScore,lastDefendFailScoreTime,worshipTime,remainMatchTimes,lastMatchTime,totalMatchTimes,cricketMatchData,guide,drawPieceCount,drawLevel,drawRewardLevel,drawReward,guideBit,totalOneCatchTimes,totalTenCatchTimes,courtRound,courtBadgeId,courtBadgeExp,buySliverToken,buyGoldToken,tokenExp,tokenRound,skillRecycleTime,showCourtRank) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserCricketData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCatchEventTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCricketIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBadgeLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCricketScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScenceId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getScenceDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(data.getItems())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalCatchTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurrentSeasonId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRankScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxRankScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDefendFailScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastDefendFailScoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getWorshipTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRemainMatchTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastMatchTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalMatchTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCricketMatchData())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGuide()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDrawPieceCount()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawRewardLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(data.getDrawReward())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGuideBit()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalOneCatchTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalTenCatchTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCourtRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCourtBadgeId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCourtBadgeExp()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuySliverToken()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuyGoldToken()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTokenExp()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTokenRound()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSkillRecycleTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isShowCourtRank()));
        if (getDb().executeNoneQuery(insertCricketUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCricketUserDataSql = "update `t_u_cricket_user` set `catchEventTimes`=?,`cricketIdList`=?,`badgeLevel`=?,`cricketScore`=?,`scenceId`=?,`scenceDataMap`=?,`items`=?,`totalCatchTimes`=?,`currentSeasonId`=?,`rankScore`=?,`maxRankScore`=?,`defendFailScore`=?,`lastDefendFailScoreTime`=?,`worshipTime`=?,`remainMatchTimes`=?, `lastMatchTime`=?,`totalMatchTimes`=?,`cricketMatchData`=?,`guide`=?,`drawPieceCount`=?,`drawLevel`=?,`drawRewardLevel`=?,`drawReward`=?,`guideBit`=?,`totalOneCatchTimes`=?,`totalTenCatchTimes`=?,`courtRound`=?,`courtBadgeId`=?,`courtBadgeExp`=?,`buySliverToken`=?,`buyGoldToken`=?,`tokenExp`=?,`tokenRound`=?,`skillRecycleTime`=?,`showCourtRank`=? where `userId`=?";

    @Override
    protected boolean updateData(UserCricketData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getCatchEventTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCricketIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBadgeLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCricketScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScenceId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getScenceDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(data.getItems())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalCatchTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurrentSeasonId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRankScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxRankScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDefendFailScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastDefendFailScoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getWorshipTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRemainMatchTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastMatchTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalMatchTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCricketMatchData())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGuide()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDrawPieceCount()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawRewardLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(data.getDrawReward())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGuideBit()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalOneCatchTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalTenCatchTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCourtRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCourtBadgeId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCourtBadgeExp()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuySliverToken()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuyGoldToken()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTokenExp()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTokenRound()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSkillRecycleTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isShowCourtRank()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateCricketUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserCricketData parseData(ResultSet rs) throws SQLException {
        UserCricketData data = new UserCricketData();
        data.setUserId(rs.getLong("userId"));
        data.setCatchEventTimes(rs.getLong("catchEventTimes"));
        data.setCricketIdList(JSON.parseObject(rs.getString("cricketIdList"), new TypeReference<List<Integer>>() {
        }));
        data.setBadgeLevel(rs.getInt("badgeLevel"));
        data.setCricketScore(rs.getLong("cricketScore"));
        data.setScenceId(rs.getInt("scenceId"));
        data.setScenceDataMap(JSON.parseObject(rs.getString("scenceDataMap"), new TypeReference<Map<Integer, CricketScenceData>>() {
        }));
        data.setItems(PropertyHelper.parseStringToProperty(rs.getString("items")));
        data.setTotalCatchTimes(rs.getLong("totalCatchTimes"));
        data.setCurrentSeasonId(rs.getInt("currentSeasonId"));
        data.setRankScore(rs.getLong("rankScore"));
        data.setMaxRankScore(rs.getLong("maxRankScore"));
        data.setDefendFailScore(rs.getLong("defendFailScore"));
        data.setLastDefendFailScoreTime(rs.getLong("lastDefendFailScoreTime"));
        data.setWorshipTime(rs.getLong("worshipTime"));
        data.setRemainMatchTimes(rs.getInt("remainMatchTimes"));
        data.setLastMatchTime(rs.getLong("lastMatchTime"));
        data.setTotalMatchTimes(rs.getInt("totalMatchTimes"));
        data.setCricketMatchData(JSON.parseObject(rs.getString("cricketMatchData"), new TypeReference<CricketMatchData>() {
        }));
        if (data.getCricketMatchData() == null) {
            data.setCricketMatchData(CricketMatchData.getInstance());
        }
        data.setGuide(rs.getInt("guide"));
        data.setDrawPieceCount(rs.getLong("drawPieceCount"));
        data.setDrawLevel(rs.getInt("drawLevel"));
        data.setDrawRewardLevel(rs.getInt("drawRewardLevel"));
        data.setDrawReward(PropertyHelper.parseStringToProperty(rs.getString("drawReward")));
        data.setGuideBit(rs.getInt("guideBit"));
        data.setTotalOneCatchTimes(rs.getInt("totalOneCatchTimes"));
        data.setTotalTenCatchTimes(rs.getInt("totalTenCatchTimes"));
        data.setCourtRound(rs.getInt("courtRound"));
        data.setCourtBadgeId(rs.getInt("courtBadgeId"));
        data.setCourtBadgeExp(rs.getLong("courtBadgeExp"));
        data.setBuySliverToken(rs.getInt("buySliverToken"));
        data.setBuyGoldToken(rs.getInt("buyGoldToken"));
        data.setTokenExp(rs.getInt("tokenExp"));
        data.setTokenRound(rs.getInt("tokenRound"));
        data.setSkillRecycleTime(rs.getLong("skillRecycleTime"));
        data.setShowCourtRank(rs.getBoolean("showCourtRank"));
        return data;
    }
}
